package com.example.ujsfish.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.ujsfish.R;
import com.example.ujsfish.utils.AppNetService;
import com.example.ujsfish.utils.DeviceName;
import com.example.ujsfish.utils.RetrofitManager;

import java.util.Timer;
import java.util.TimerTask;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class CmAuto extends AppCompatActivity {

    private static final String TAG = "CmAuto";
    Button btn_oCalibration;
    Button btn_Geomagnetic_calibration;
    Button btn_sprayCruise;
    Button btn_grain_BaitCruise;
    Button btn_fish_BaitCruise;
    Button btn_sprayRe;
    Button btn_baitRe;
    Button btn_newPoolByJW;
    Button btn_ReWeighing;
    Button btn_mapPaint;
    Button btn_pathPlanning;
    Button btn_toManual;
    Button btn_toSemiAuto;
    Button btn_cruisePause;
    Button btn_mapPaint2;
    ImageButton btn_MonitorData;
    ImageButton btn_CurrentPath;


    RetrofitManager myRetrofitManager = RetrofitManager.getInstance();
    Retrofit myRetrofit = RetrofitManager.getRetrofit();
    AppNetService appNetService = myRetrofitManager.getApiService(AppNetService.class);
    Timer timer;
    TimerTask task;

    String id = DeviceName.getDeviceInstance().getDeviceId().get(0);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cm_auto_activity);
        myRetrofitManager = RetrofitManager.getInstance();
        myRetrofit = RetrofitManager.getRetrofit();
        AppNetService appNetService = myRetrofitManager.getApiService(AppNetService.class);
        System.out.println(id);
        initView();
        //btn_mapDownload.setEnabled(false);
        //btn_baitRe.setEnabled(false);
        //btn_sprayRe.setEnabled(false);
        btn_oCalibration.setEnabled(false);
        btn_ReWeighing.setEnabled(false);

        setBtnListener(); 

    }

    //原点校准
    private void setBtnListener() {

        btn_ReWeighing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<ResponseBody> responseBodyCall = appNetService.postReWeighing(id);
                responseBodyCall.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        Toast.makeText(getApplication(),"重量校零成功",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });
            }
        });

        btn_newPoolByJW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CmAuto.this,CreateBoundaryByMan.class);
                startActivity(intent);
            }
        });
        btn_oCalibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Call<ResponseBody> call = appNetService.postOrigin_calibration(id);
                callEnqueue(call);
            }

        });

        //地磁校准
        btn_Geomagnetic_calibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Call<ResponseBody> responseBodyCall = appNetService.postGeomagnetic_calibration(id);
                responseBodyCall.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });
            }
        });

        //施药巡航
        btn_sprayCruise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Call<ResponseBody> call = appNetService.postSpraying_Cruise(id);
//                callEnqueue(call);
                Intent intent = new Intent(CmAuto.this,CruiseActivity.class);
                intent.putExtra("type","spray");
                startActivity(intent);
            }
        });

        //颗粒饵料巡航
        btn_grain_BaitCruise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Call<ResponseBody> call = appNetService.postFeeding_Cruise(id);
//                callEnqueue(call);

                Intent intent = new Intent(CmAuto.this,CruiseActivity.class);
                intent.putExtra("type","grain");
                startActivity(intent);
            }
        });

        //杂鱼巡航
        btn_fish_BaitCruise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Call<ResponseBody> call = appNetService.postFeeding_Cruise(id);
//                callEnqueue(call);
                Intent intent = new Intent(CmAuto.this,CruiseActivity.class);
                intent.putExtra("type","fish");
                startActivity(intent);
            }
        });

        btn_sprayRe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Call<ResponseBody> call = appNetService.postSprayingRe(id);
                callEnqueue(call);
            }
        });

        btn_baitRe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Call<ResponseBody> call = appNetService.postBaitingRe(id);
                callEnqueue(call);
            }
        });


        btn_mapPaint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CmAuto.this, PaintMap.class);
                startActivity(intent);
            }
        });

        btn_pathPlanning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CmAuto.this, AddPathPoint.class);
                startActivity(intent);
            }
        });

        btn_cruisePause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<ResponseBody> call = appNetService.postCruisePause(id);
                callEnqueue(call);

            }
        });

        btn_toManual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CmAuto.this,CmManual.class);
                startActivity(intent);
            }
        });
        btn_toSemiAuto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CmAuto.this,CmSemiAuto.class);
                startActivity(intent);
            }
        });
        btn_MonitorData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CmAuto.this, MonitorActivity.class);
                startActivity(intent);
            }
        });

        btn_CurrentPath.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CmAuto.this, com.example.ujsfish.activity.CreateMap.class);
                startActivity(intent);
            }
        });
        btn_mapPaint2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CmAuto.this, com.example.ujsfish.activity.PaintMapByMan.class);
                startActivity(intent);
            }
        });




    }

    private void callEnqueue(Call<ResponseBody> call) {
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.d(TAG, "onResponse: " + response);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void initView() {
         btn_oCalibration  = findViewById(R.id.btn_oCalibration);
         btn_Geomagnetic_calibration = findViewById(R.id.btn_Geomagnetic_calibration);
         btn_sprayCruise   = findViewById(R.id.btn_sprayCruise);
         btn_grain_BaitCruise    = findViewById(R.id.btn_grainBait);
         btn_fish_BaitCruise    = findViewById(R.id.btn_fishBait);
         btn_sprayRe       = findViewById(R.id.btn_sprayRe);
         btn_baitRe        = findViewById(R.id.btn_baitRe);
         btn_newPoolByJW = findViewById(R.id.btn_newPoolByJW);
         btn_ReWeighing = findViewById(R.id.btn_ReWeighing);
         btn_mapPaint      = findViewById(R.id.btn_mapPaint);
         btn_mapPaint2      = findViewById(R.id.btn_mapPaint2);
         btn_pathPlanning  = findViewById(R.id.btn_pathPlanning);
         btn_toManual      = findViewById(R.id.btn_toManual);
         btn_toSemiAuto    = findViewById(R.id.btn_toSemiAuto);
         btn_cruisePause   = findViewById(R.id.btn_cruisePause);
         btn_MonitorData   = findViewById(R.id.btn_MonitorData);
         btn_CurrentPath   = findViewById(R.id.btn_CurrentPath);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        timer = new Timer();
//        task = new TimerTask() {
//            @Override
//            public void run() {
//
//            }
//        };
//        //timer.schedule(task,0,1000);
//        System.out.println("定时器开启");

    }

    @Override
    protected void onPause() {
        super.onPause();
//        timer.cancel();
//        System.out.println("定时器关闭");
    }


}